//
//  NSDate+extension.swift
//
//  Created by laichunhui on 15/10/13.
//  Copyright © 2015年 lch. All rights reserved.
//
import Foundation

public extension NSDate {
    
    /**
     Date year
     */
    public var year : Int {
        get {
            return getComponent(NSCalendarUnit.Year)
        }
    }
    
    /**
     Date month
     */
    public var month : Int {
        get {
            return getComponent(NSCalendarUnit.Month)
        }
    }
    
    /**
     English form of the month
     */
    public var en_month : String {
        get {
            let month = getComponent(NSCalendarUnit.Month)
            
            switch month {
            case 1:
                return "January"
            case 2:
                return "February"
            case 3:
                return "March"
            case 4:
                return "April"
            case 5:
                return "May"
            case 6:
                return "June"
            case 7:
                return "July"
            case 8:
                return "August"
            case 9:
                return "September"
            case 10:
                return "October"
            case 11:
                return "November"
            case 12:
                return "December"
            default:
                return "\(month) not one month"
            }
        }
    }
    
    /**
     Date days
     */
    public var days : Int {
        get {
            return getComponent(NSCalendarUnit.Day)
        }
    }

    /**
     Date hours
     */
    public var hours : Int {
        
        get {
            return getComponent(NSCalendarUnit.Hour)
        }
    }
    
    /**
     Date minuts
     */
    public var minutes : Int {
        get {
            return getComponent(NSCalendarUnit.Minute)
        }
    }
    
    /**
     Date seconds
     */
    public var seconds : Int {
        get {
            return getComponent(NSCalendarUnit.Second)
        }
    }
    
    /**
     Returns the value of the NSDate component
     
     :param: component NSCalendarUnit
     :returns: the value of the component
     */
    
    public func getComponent (component : NSCalendarUnit) -> Int {
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(component, fromDate: self)
        
        return components.valueForComponent(component)
    }
    

    
    /// 将新浪微博日期格式的字符串转换成 NSDate
    class func sinaDate(string: String) -> NSDate? {
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        
        return df.dateFromString(string)
    }
    
    /// 将yuejie日期格式的字符串转换成 NSDate  "2015-11-08 17:30:00"
    class func yuejieDate(string: String) -> NSDate? {
        let df = NSDateFormatter()
      //  df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        return df.dateFromString(string)
    }
    
    
    
    /**
        根据当前日期，返回对应的格式描述文字
        
        刚刚(一分钟内)
        X分钟前(一小时内)
        X小时前(当天)
        昨天 HH:mm(昨天)
        MM-dd HH:mm(今年内)
        yyyy-MM-dd HH:mm(去年以前)
    */
    var dateDescription: String {
        
        // 获取当前日历 NSCalendar 提供了非常丰富的日历处理函数
        let canlender = NSCalendar.currentCalendar()
        
        if canlender.isDateInToday(self) {
            
            // 计算指定日期和当前系统时间的时间差
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            
            if delta < 60 {
                return "刚刚"
            }
            if delta < 3600 {
                return "\(delta / 60) 分钟前"
            }
            
            return "\(delta / 3600) 小时前"
        }
        
        // 更早期的处理
        var fmt = " HH:mm"
        
        if canlender.isDateInYesterday(self) {
            fmt = "昨天" + fmt
        } else {
            
            fmt = "MM-dd" + fmt
            
            // 单纯根据年的数字判断
            // print(canlender.component(.Year, fromDate: self))
            // 使用对比，能够判断两个日期之间是否有一个完整的`年度`差
            let coms = canlender.components(.Year, fromDate: self, toDate: NSDate(), options: [])
            
            if coms.year != 0 {
                fmt = "yyyy-" + fmt
            }
        }
        
        // 日期转换
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = fmt
        
        return df.stringFromDate(self)
    }
}

